package com.behome.netty.netty_B.bean;


import com.behome.netty.netty_B.enums.ReBackAnnotation;

/**
 * @author bh_ljy
 * @description: 附录B回传主业务类
 * @date: 2024/3/29 13:52
 */
public abstract class BusinessReBackBase {
    private int sessionId = 0;

    public BusinessReBackBase() {
    }

    public abstract boolean validateBeforeSerialize();

    public abstract byte[] serialize();

    public abstract BusinessReBackBase deserialize(byte[] var1);

    public abstract String toCHJsonString();

    public ReBackPacket generateSendPacket(int sessionId, String sourceCode, String[] targetCodes) {
        ReBackPacket packet = new ReBackPacket();
        ReBackPacketBody body = new ReBackPacketBody(sourceCode, targetCodes, this);
        packet.setBody(body);
        ReBackPacketHead head = new ReBackPacketHead(sessionId, ReBackPacketHead.PacketType.REBACK, (new Integer(11 + body.serialize().length + 4)).shortValue());
        packet.setHead(head);
        ReBackPacketTail tail = new ReBackPacketTail(head, body);
        packet.setTail(tail);
        return packet;
    }

    public String getReBackInfo() {
        String ret = "未知";
        ReBackAnnotation annotation = this.getClass().getAnnotation(ReBackAnnotation.class);
        if (annotation != null) {
            ret = annotation.type().toString();
        }

        return ret;
    }

    public byte getReBackTypeValue() {
        ReBackAnnotation annotation = this.getClass().getAnnotation(ReBackAnnotation.class);
        return annotation != null ? annotation.type().getValue() : 0;
    }

    public String getReBackTypeName() {
        ReBackAnnotation annotation = this.getClass().getAnnotation(ReBackAnnotation.class);
        return annotation != null ? annotation.type().getName() : "未知类型";
    }

    public String getSendCmdInfo() {
        String ret = "(会话标识:{sessionId},指令信息:{Info})";
        ret = ret.replace("{sessionId}", this.sessionId + "").replace("{Info}", this.getReBackInfo());
        return ret;
    }

}